home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectSound / Play3DSound / Sound3D.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  15.9 KB  |  447 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form DS3DPositionForm 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "DS 3D Positioning"
  6.    ClientHeight    =   5565
  7.    ClientLeft      =   930
  8.    ClientTop       =   330
  9.    ClientWidth     =   5055
  10.    Icon            =   "Sound3D.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   5565
  15.    ScaleWidth      =   5055
  16.    Begin VB.Timer tmrUpdate 
  17.       Interval        =   50
  18.       Left            =   4260
  19.       Top             =   2100
  20.    End
  21.    Begin MSComDlg.CommonDialog cdlFile 
  22.       Left            =   3780
  23.       Top             =   2040
  24.       _ExtentX        =   847
  25.       _ExtentY        =   847
  26.       _Version        =   393216
  27.    End
  28.    Begin VB.PictureBox picDraw 
  29.       BackColor       =   &H00FFFFFF&
  30.       FillStyle       =   7  'Diagonal Cross
  31.       Height          =   2775
  32.       Left            =   120
  33.       ScaleHeight     =   181
  34.       ScaleMode       =   3  'Pixel
  35.       ScaleWidth      =   317
  36.       TabIndex        =   7
  37.       TabStop         =   0   'False
  38.       Top             =   2640
  39.       Width           =   4815
  40.    End
  41.    Begin VB.PictureBox picContainer 
  42.       Height          =   1755
  43.       Index           =   0
  44.       Left            =   120
  45.       ScaleHeight     =   1695
  46.       ScaleWidth      =   4755
  47.       TabIndex        =   10
  48.       TabStop         =   0   'False
  49.       Top             =   120
  50.       Width           =   4815
  51.       Begin VB.TextBox txtSound 
  52.          BackColor       =   &H8000000F&
  53.          Height          =   315
  54.          Left            =   960
  55.          Locked          =   -1  'True
  56.          TabIndex        =   13
  57.          Top             =   120
  58.          Width           =   3735
  59.       End
  60.       Begin VB.CommandButton cmdSound 
  61.          Caption         =   "Sound..."
  62.          Enabled         =   0   'False
  63.          Height          =   315
  64.          Left            =   60
  65.          TabIndex        =   0
  66.          Top             =   120
  67.          Width           =   855
  68.       End
  69.       Begin VB.CommandButton cmdPlay 
  70.          Caption         =   "Play"
  71.          Height          =   375
  72.          Left            =   120
  73.          TabIndex        =   3
  74.          Top             =   1200
  75.          Width           =   855
  76.       End
  77.       Begin VB.CommandButton cmdPause 
  78.          Caption         =   "Pause"
  79.          Height          =   375
  80.          Left            =   1020
  81.          TabIndex        =   4
  82.          Top             =   1200
  83.          Width           =   855
  84.       End
  85.       Begin VB.CommandButton cmdStop 
  86.          Caption         =   "Stop"
  87.          Height          =   375
  88.          Left            =   1920
  89.          TabIndex        =   5
  90.          Top             =   1200
  91.          Width           =   735
  92.       End
  93.       Begin VB.CheckBox chLoop 
  94.          Caption         =   "Loop Play"
  95.          Height          =   315
  96.          Left            =   2760
  97.          TabIndex        =   6
  98.          Top             =   1260
  99.          Width           =   1455
  100.       End
  101.       Begin VB.HScrollBar scrlVol 
  102.          Height          =   255
  103.          LargeChange     =   20
  104.          Left            =   840
  105.          Max             =   0
  106.          Min             =   -3000
  107.          SmallChange     =   500
  108.          TabIndex        =   1
  109.          Top             =   540
  110.          Width           =   3855
  111.       End
  112.       Begin VB.HScrollBar scrlAngle 
  113.          Height          =   255
  114.          LargeChange     =   20
  115.          Left            =   840
  116.          Max             =   360
  117.          Min             =   -360
  118.          SmallChange     =   10
  119.          TabIndex        =   2
  120.          Top             =   840
  121.          Value           =   -90
  122.          Width           =   3855
  123.       End
  124.       Begin VB.Label Label1 
  125.          BackStyle       =   0  'Transparent
  126.          Caption         =   "Volume"
  127.          Height          =   255
  128.          Index           =   0
  129.          Left            =   120
  130.          TabIndex        =   12
  131.          Top             =   600
  132.          Width           =   1095
  133.       End
  134.       Begin VB.Label Label2 
  135.          BackStyle       =   0  'Transparent
  136.          Caption         =   "Direction"
  137.          Height          =   255
  138.          Index           =   0
  139.          Left            =   120
  140.          TabIndex        =   11
  141.          Top             =   900
  142.          Width           =   975
  143.       End
  144.    End
  145.    Begin VB.Label Label5 
  146.       BackStyle       =   0  'Transparent
  147.       Caption         =   "Click and drag the red triangle around with the left mouse button to change the sound position."
  148.       Height          =   495
  149.       Left            =   120
  150.       TabIndex        =   9
  151.       Top             =   2160
  152.       Width           =   4755
  153.    End
  154.    Begin VB.Label Label4 
  155.       BackStyle       =   0  'Transparent
  156.       Caption         =   "Sound Positions"
  157.       BeginProperty Font 
  158.          Name            =   "MS Sans Serif"
  159.          Size            =   8.25
  160.          Charset         =   0
  161.          Weight          =   700
  162.          Underline       =   0   'False
  163.          Italic          =   0   'False
  164.          Strikethrough   =   0   'False
  165.       EndProperty
  166.       Height          =   375
  167.       Left            =   120
  168.       TabIndex        =   8
  169.       Top             =   1920
  170.       Width           =   1575
  171.    End
  172. Attribute VB_Name = "DS3DPositionForm"
  173. Attribute VB_GlobalNameSpace = False
  174. Attribute VB_Creatable = False
  175. Attribute VB_PredeclaredId = True
  176. Attribute VB_Exposed = False
  177. Option Explicit
  178. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  179. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  180. '  File:       Sound3d.frm
  181. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  182. 'API declare for windows folder
  183. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  184. Dim dx As New DirectX8 'Our DirectX object
  185. Dim ds As DirectSound8 'Our DirectSound object
  186. Dim dsBuffer As DirectSoundSecondaryBuffer8 'Our SoundBuffer
  187. Dim ds3dBuffer As DirectSound3DBuffer8 'We need to get a 3DSoundBuffer
  188. Dim oPos As D3DVECTOR 'Position
  189. Dim fMouseDown As Boolean 'Is the mouse down?
  190. Private Sub cmdSound_Click()
  191.     Static sCurDir As String
  192.     Static lFilter As Long
  193.     Dim dsBuf As DSBUFFERDESC
  194.     'Now we should load a wave file
  195.     'Ask them for a file to load
  196.     With cdlFile
  197.         .flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly
  198.         .FilterIndex = lFilter
  199.         .Filter = "Wave Files (*.wav)|*.wav"
  200.         .FileName = vbNullString
  201.         If sCurDir = vbNullString Then
  202.             'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  203.             Dim sWindir As String
  204.             sWindir = Space$(255)
  205.             If GetWindowsDirectory(sWindir, 255) = 0 Then
  206.                 'We couldn't get the windows folder for some reason, use the c:\
  207.                 .InitDir = "C:\"
  208.             Else
  209.                 Dim sMedia As String
  210.                 sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  211.                 If Right$(sWindir, 1) = "\" Then
  212.                     sMedia = sWindir & "Media"
  213.                 Else
  214.                     sMedia = sWindir & "\Media"
  215.                 End If
  216.                 If Dir$(sMedia, vbDirectory) <> vbNullString Then
  217.                     .InitDir = sMedia
  218.                 Else
  219.                     .InitDir = sWindir
  220.                 End If
  221.             End If
  222.         Else
  223.             .InitDir = sCurDir
  224.         End If
  225.         .ShowOpen   ' Display the Open dialog box
  226.         If .FileName = vbNullString Then
  227.             Exit Sub 'We didn't click anything exit
  228.         End If
  229.         'Save the current information
  230.         sCurDir = GetFolder(.FileName)
  231.         lFilter = .FilterIndex
  232.         
  233.         'Save the filename for later use
  234.         If Not (dsBuffer Is Nothing) Then dsBuffer.Stop
  235.         Set dsBuffer = Nothing
  236.         txtSound.Text = vbNullString
  237.         dsBuf.lFlags = DSBCAPS_CTRL3D Or DSBCAPS_CTRLVOLUME
  238.         'Before we load the 3D dialog check to see if this is a mono file
  239.         On Error Resume Next
  240.         Set dsBuffer = ds.CreateSoundBufferFromFile(.FileName, dsBuf)
  241.         If Err Then
  242.             'First check to see if this is a stereo wav file
  243.             If (dsBuf.fxFormat.nChannels > 1) And (Err.Number = 5) Then 'Yup
  244.                 MsgBox "You must load a mono wave file to control 3D sound.  Stereo wave files are not supported.", vbOKOnly Or vbInformation, "Couldn't load"
  245.             Else
  246.                 MsgBox "Could not load this wave file." & vbCrLf & "Format is not supported.", vbOKOnly Or vbInformation, "Couldn't load"
  247.             End If
  248.             Exit Sub
  249.         End If
  250.         
  251.         'Now we need to get the 3D virtualization params
  252.         Dim f3DParams As New frm3DAlg
  253.         
  254.         f3DParams.Show vbModal, Me
  255.         If f3DParams.OKHit Then
  256.             If f3DParams.optFull Then dsBuf.guid3DAlgorithm = GUID_DS3DALG_HRTF_FULL
  257.             If f3DParams.optHalf Then dsBuf.guid3DAlgorithm = GUID_DS3DALG_HRTF_LIGHT
  258.             If f3DParams.optNone Then dsBuf.guid3DAlgorithm = GUID_DS3DALG_NO_VIRTUALIZATION
  259.         Else
  260.             Set dsBuffer = Nothing
  261.             Exit Sub
  262.         End If
  263.         On Error Resume Next
  264.         Set dsBuffer = ds.CreateSoundBufferFromFile(.FileName, dsBuf)
  265.         If Err Then
  266.             MsgBox "Could not create the sound buffer.", vbOKOnly Or vbInformation, "Couldn't load"
  267.             Exit Sub
  268.         End If
  269.         txtSound.Text = .FileName
  270.         EnablePlayUI True
  271.         Set ds3dBuffer = dsBuffer.GetDirectSound3DBuffer
  272.         ds3dBuffer.SetConeAngles DS3D_MINCONEANGLE, 100, DS3D_IMMEDIATE
  273.         ds3dBuffer.SetConeOutsideVolume -400, DS3D_IMMEDIATE
  274.         ' position our sound
  275.         ds3dBuffer.SetPosition oPos.x / 50, 0, oPos.z / 50, DS3D_IMMEDIATE
  276.         'Update the volume
  277.         scrlVol_Change
  278.     End With
  279. End Sub
  280. Private Sub Form_Load()
  281.     On Local Error Resume Next
  282.     Set ds = dx.DirectSoundCreate(vbNullString) 'Create a default DirectSound object
  283.     'We couldn't create the DSound object.  End the app now
  284.     If Err.Number <> 0 Then
  285.         MsgBox "Could not initialize DirectSound." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  286.         Unload Me
  287.         End
  288.     End If
  289.     'Set the coop level
  290.     ds.SetCooperativeLevel Me.hWnd, DSSCL_PRIORITY
  291.     'Show the form
  292.     Me.Show
  293.     oPos.x = 0: oPos.z = 5
  294.     '- Make sure we pickup the correct volume and orientation
  295.     scrlAngle_Change
  296.     scrlVol_Change
  297.     DrawPositions
  298.     EnablePlayUI True
  299.     cmdPlay.Enabled = False
  300.     cmdSound.SetFocus
  301. End Sub
  302. Private Sub cmdPlay_Click()
  303.     If dsBuffer Is Nothing Then Exit Sub
  304.            
  305.     'Play plays the sound from the current position
  306.     'if the sound was paused using the stop command
  307.     'then play will begin where it last left off
  308.     dsBuffer.Play chLoop.Value 'Checked = 1 (looping), Unchecked = 0 (Default)
  309.     EnablePlayUI False
  310. End Sub
  311. Private Sub cmdStop_Click()
  312.     If dsBuffer Is Nothing Then Exit Sub
  313.     dsBuffer.Stop
  314.     dsBuffer.SetCurrentPosition 0 'Reset the position since Stop doesn't
  315.     EnablePlayUI True
  316. End Sub
  317. Private Sub cmdPause_Click()
  318.     If dsBuffer Is Nothing Then Exit Sub
  319.     dsBuffer.Stop 'Stop doesn't reset the position
  320. End Sub
  321. 'They've changed the volume.  Update it
  322. Private Sub scrlVol_Change()
  323.     If dsBuffer Is Nothing Then Exit Sub
  324.     dsBuffer.SetVolume scrlVol.Value
  325. End Sub
  326. Private Sub scrlVol_Scroll()
  327.     scrlVol_Change
  328. End Sub
  329. 'They've changed the angle.  Update it
  330. Private Sub scrlAngle_Change()
  331.     'We need to calculate a vector of what direction the sound is traveling in.
  332.     Dim x As Single
  333.     Dim z As Single
  334.     'we take the current angle in degrees convert to radians
  335.     'and get the cos or sin to find the direction from an angle
  336.     x = 5 * Cos(3.141 * scrlAngle.Value / 180)
  337.     z = 5 * Sin(3.141 * scrlAngle.Value / 180)
  338.     'Update the UI
  339.     DrawPositions
  340.     If dsBuffer Is Nothing Then Exit Sub
  341.     ds3dBuffer.SetConeOrientation x, 0, z, DS3D_IMMEDIATE
  342. End Sub
  343. Private Sub scrlAngle_Scroll()
  344.     scrlAngle_Change
  345. End Sub
  346. Sub UpdatePosition(x As Single, z As Single)
  347.     On Error Resume Next
  348.     oPos.x = x - picDraw.ScaleWidth / 2
  349.     oPos.z = z - picDraw.ScaleHeight / 2
  350.     DrawPositions
  351.     'the zero at the end indicates we want the postion updated immediately
  352.     If ds3dBuffer Is Nothing Then Exit Sub
  353.     ds3dBuffer.SetPosition oPos.x / 50, 0, oPos.z / 50, DS3D_IMMEDIATE
  354. End Sub
  355. Private Sub picDraw_MouseDown(Button As Integer, Shift As Integer, x As Single, z As Single)
  356.     On Error Resume Next
  357.     If Button = vbLeftButton Then
  358.         UpdatePosition x, z
  359.         fMouseDown = True
  360.     End If
  361. End Sub
  362. Private Sub picDraw_MouseMove(Button As Integer, Shift As Integer, x As Single, z As Single)
  363.     On Error Resume Next
  364.     If Not fMouseDown Then Exit Sub
  365.     If Button = vbLeftButton Then
  366.         'Only update the position if it is outside of the box
  367.         If x < 0 Or z < 0 Or x > picDraw.ScaleWidth Or z > picDraw.ScaleHeight Then Exit Sub
  368.         UpdatePosition x, z
  369.     End If
  370. End Sub
  371. Private Sub picDraw_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
  372.     On Error Resume Next
  373.     fMouseDown = False
  374. End Sub
  375. Private Sub picDraw_Paint()
  376.     DrawPositions
  377. End Sub
  378. Sub DrawPositions()
  379.     Dim x As Integer
  380.     Dim z As Integer
  381.     picDraw.Cls
  382.     'listener is in center and is black
  383.     DrawTriangle 0, picDraw.ScaleWidth / 2, picDraw.ScaleHeight / 2, 90
  384.     'draw sound as RED
  385.     x = CInt(oPos.x) + picDraw.ScaleWidth / 2
  386.     z = CInt(oPos.z) + picDraw.ScaleHeight / 2
  387.     DrawTriangle RGB(256, 0, 0), x, z, scrlAngle.Value
  388. End Sub
  389. 'Draw a triangle representing where we are
  390. Sub DrawTriangle(col As Long, x As Integer, z As Integer, ByVal a As Single)
  391.     Dim x1 As Integer
  392.     Dim z1 As Integer
  393.     Dim x2 As Integer
  394.     Dim z2 As Integer
  395.     Dim x3 As Integer
  396.     Dim z3 As Integer
  397.     a = 3.141 * (a - 90) / 180
  398.     Dim q As Integer
  399.     q = 10
  400.     x1 = q * Sin(a) + x
  401.     z1 = q * Cos(a) + z
  402.     x2 = q * Sin(a + 3.141 / 1.3) + x
  403.     z2 = q * Cos(a + 3.141 / 1.3) + z
  404.     x3 = q * Sin(a - 3.141 / 1.3) + x
  405.     z3 = q * Cos(a - 3.141 / 1.3) + z
  406.     picDraw.Line (x1, z1)-(x2, z2), col
  407.     picDraw.Line (x1, z1)-(x3, z3), col
  408.     picDraw.Line (x2, z2)-(x3, z3), col
  409. End Sub
  410. Private Function GetFolder(ByVal sFile As String) As String
  411.     Dim lCount As Long
  412.     For lCount = Len(sFile) To 1 Step -1
  413.         If Mid$(sFile, lCount, 1) = "\" Then
  414.             GetFolder = Left$(sFile, lCount)
  415.             Exit Function
  416.         End If
  417.     Next
  418.     GetFolder = vbNullString
  419. End Function
  420. Private Sub EnablePlayUI(ByVal fEnable As Boolean)
  421.     On Error Resume Next
  422.     If fEnable Then
  423.         chLoop.Enabled = True
  424.         cmdPlay.Enabled = True
  425.         cmdPause.Enabled = False
  426.         cmdStop.Enabled = False
  427.         cmdSound.Enabled = True
  428.         cmdPlay.SetFocus
  429.     Else
  430.         chLoop.Enabled = False
  431.         cmdPlay.Enabled = False
  432.         cmdStop.Enabled = True
  433.         cmdPause.Enabled = True
  434.         cmdSound.Enabled = False
  435.         cmdStop.SetFocus
  436.     End If
  437. End Sub
  438. Private Sub tmrUpdate_Timer()
  439.     If Not (dsBuffer Is Nothing) Then
  440.         If (dsBuffer.GetStatus And DSBSTATUS_PLAYING) <> DSBSTATUS_PLAYING Then
  441.             If cmdPlay.Enabled = False Then
  442.                 EnablePlayUI True
  443.             End If
  444.         End If
  445.     End If
  446. End Sub
  447.